/*
 *  MainComponent.cpp
 *  Typeface Builder
 *
 *  Created by David Rowland on 01/12/2008.
 *  Copyright 2008 UWE. All rights reserved.
 *
 */

#include "MainComponent.h"
#include "FontSerialiser.cpp"
#include "BinaryBuilder.cpp"

MainComponent::MainComponent ()
{
	addAndMakeVisible( fontListBox = new FontListBox() );
	addAndMakeVisible( userSettings = new UserSettings() );
	
	addAndMakeVisible( buildButton = new TextButton(T("Build"), T("Build the selected font into a binary file")) );
	buildButton->addButtonListener(this);
	buildButton->changeWidthToFitText(25);
	
	addAndMakeVisible( textConsole = new TextConsole() );
	String noSystemFonts = (String)fontListBox->getNumRows();
	textConsole->addLine( String("The number of installed system fonts is: ") << noSystemFonts );
}
	
MainComponent::~MainComponent ()
{
	deleteAllChildren();
}
	
//==============================================================================
void MainComponent::resized ()
{
	fontListBox->setBounds(0, 0, getWidth()/2 - 5, getHeight());
	userSettings->setBounds(getWidth()/2 + 5, 0, getWidth()/2 - 5, 150);
	
	buildButton->setTopLeftPosition(getWidth() - buildButton->getWidth(),
									userSettings->getBottom());
	
	textConsole->setBounds(getWidth()/2 + 5,
						   buildButton->getBottom() + 10,
						   getWidth()/2 - 5,
						   getHeight() - buildButton->getHeight() - userSettings->getHeight() - 10);
}
	
void MainComponent::paint (Graphics& g)
{
}
	
void MainComponent::repaint()
{
	
}

//==============================================================================
// Callback methods
//==============================================================================

void MainComponent::buttonClicked (Button* clickedButton)
{
	if (clickedButton == buildButton)
	{
		textConsole->addLine(String("The selected font is: ") << fontListBox->getSelectedFont());
		
		if(userSettings->binaryFileName->getText() != String::empty)
		{
			File fontFile = serialiseFont(userSettings->binaryFileName->getText(),				// filename
										  fontListBox->getSelectedFont(),						// fontname
										  userSettings->destinationDirectory->getCurrentFile(), // destination folder
										  textConsole);											// reference to the text console
			
			// Check to see if the font file is valid
			if (fontFile != File::nonexistent)
			{
				//Usage: BinaryBuilder  sourcedirectory targetdirectory targetclassname [optional wildcard pattern]
				StringArray binaryBuilderInput;
				binaryBuilderInput.add("BinaryBuilder");
				binaryBuilderInput.add(userSettings->destinationDirectory->getCurrentFile().getFullPathName());
				binaryBuilderInput.add(userSettings->destinationDirectory->getCurrentFile().getFullPathName());
				binaryBuilderInput.add(userSettings->binaryFileName->getText());
				DBG_PRINTF(( String("The size of the string array is: ") << String(binaryBuilderInput.size()) ));
				
				binaryBuilderMain(binaryBuilderInput.size(), binaryBuilderInput, fontFile, textConsole);
				
				// Delete temp font file
				if (userSettings->deleteTempButton->getToggleState())
				{
					fontFile.deleteFile();
					textConsole->addLine("Temp font file deleted.");
				}
				
				textConsole->addLine("\n\n---Operation successfully completed---\n\n");
			}
		}
		else
			textConsole->addLine("\n***Please enter a name for the output file.\n");
	}
}